package com.mhsoft.fenak;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mhsoft.fenak.utils.ConnectionManager;
import com.mhsoft.fenak.utils.Constants;
import com.mhsoft.fenak.utils.JSONParser;
import com.mhsoft.fenak.utils.TextValidation;
import com.mhsoft.fenak.utils.UpdateUI_Interface;

public class RegistrationActivity extends Activity implements OnClickListener, UpdateUI_Interface{
	
	private EditText firstNameEditText, lastNameEditText, emailEditText, passEditText, confPassEditText, usernameEditText, mobileEditText;
	private Button okButton;
	
	public final static int REQ_NORMAL_REGISTER = 0;
	public final static int REQ_SIGN_IN = 1;
	
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.sign_up);
        
        firstNameEditText=(EditText)findViewById(R.id.signup_firstname_editText);
        lastNameEditText=(EditText)findViewById(R.id.signup_lastname_editText);
        emailEditText=(EditText)findViewById(R.id.signup_email_editText);
        passEditText=(EditText)findViewById(R.id.signup_pass_editText);
        confPassEditText=(EditText)findViewById(R.id.signup_confpass_editText);
        usernameEditText=(EditText)findViewById(R.id.signup_username_editText);
        mobileEditText=(EditText)findViewById(R.id.signup_mobile_editText);
        
        okButton=(Button)findViewById(R.id.signup_submit_button);
        okButton.setOnClickListener(this);
    }
	@Override
	public void onClick(View v) {
		
		if(v==okButton){
			
			if (TextValidation.validateRegisteration(this, firstNameEditText.getText().toString() 
					, lastNameEditText.getText().toString() , emailEditText.getText().toString()
					, usernameEditText.getText().toString(), passEditText.getText().toString() 
					, confPassEditText.getText().toString(), mobileEditText.getText().toString())) {
				
//				ArrayList<NameValuePair> dataString=new ArrayList<NameValuePair>();
//				dataString.add(new BasicNameValuePair("user_fname",firstNameEditText.getText().toString()));
//				dataString.add(new BasicNameValuePair("user_lname",lastNameEditText.getText().toString()));
//				dataString.add(new BasicNameValuePair("user_email", emailEditText.getText().toString()));
//				dataString.add(new BasicNameValuePair("userName",usernameEditText.getText().toString()));
//				dataString.add(new BasicNameValuePair("password", emailEditText.getText().toString()));
//				ConnectionManager.doPostRequest(this, "Registration", "Please wait...", Constants.URL_REGISTERATION, "1", dataString);
				
				ConnectionManager.doGetRequest(this, "Registration", "Please wait...", 
						Constants.URL_REGISTERATION+"?user_fname="+firstNameEditText.getText().toString()
						+"&user_lname="+lastNameEditText.getText().toString()
						+"&userName="+usernameEditText.getText().toString()
						+"&password="+passEditText.getText().toString()
						+"&user_email="+emailEditText.getText().toString()
						+"&mobile="+mobileEditText.getText().toString(), "1");
			}else{
				
			}
		}
	}
	@Override
	public void updateUI(String[] responseString) {
		
		if(responseString[0].equals("200") && Integer.parseInt(responseString[2]) == 1) {
			
			Log.d("response", responseString[1]);
			
			HashMap<String, Object> response =  (HashMap<String, Object>) JSONParser.convertJSON2Hashmap(responseString[1]);
			
			if(response.get("post") != null){
				
				if(response.get("post").equals("1")){
					
					// the user name already exists ...
					Toast.makeText(this, getString(R.string.userNameExists), Toast.LENGTH_SHORT).show();
					usernameEditText.setText("");
				}
				else if(response.get("post").equals("2")){
					
					// the email is registered ...
					Toast.makeText(this, getString(R.string.emailRegistered), Toast.LENGTH_SHORT).show();
					emailEditText.setText("");
				}
				else if(response.get("post").equals("3")){
					
					// password < 6 characters ...
					Toast.makeText(this, getString(R.string.shortPass), Toast.LENGTH_SHORT).show();
					passEditText.setText("");
					confPassEditText.setText("");
				}
				else if(response.get("post").equals("4")){
					
					// the email is invalid ...
					Toast.makeText(this, getString(R.string.emailInvalidForm), Toast.LENGTH_SHORT).show();
					emailEditText.setText("");
				}
				else if(response.get("post").equals("5")){
					
					// the mobile is already registered ...
					Toast.makeText(this, getString(R.string.mobileIsRegisterd), Toast.LENGTH_SHORT).show();
					mobileEditText.setText("");
				}
				else if(response.get("post").equals("6")){
					
					// the mobile format is invalid ...
					Toast.makeText(this, getString(R.string.invalidMobileForm), Toast.LENGTH_SHORT).show();
					mobileEditText.setText("");
				}
				else if(response.get("post").equals("7")){
					
					// db error in insert ...
					Toast.makeText(this, getString(R.string.errorInsert), Toast.LENGTH_SHORT).show();
				}
				else if(response.get("post").equals("8")){
					
					// user data inserted in db but couldn't retrieve data after insertion ...
					Toast.makeText(this, getString(R.string.successfulButRestart), Toast.LENGTH_LONG).show();
				}
			}
			else{
				
				Toast.makeText(this, getString(R.string.successfulRegistration), Toast.LENGTH_SHORT).show();
				setResult(111);
				finish();
			}
		}
		else{
			
		}
	}
}